package com.example.myspringboot.controller;

import com.example.myspringboot.bean.Teacher;
import com.example.myspringboot.dao.TeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class TeacherController {
    @Autowired
    private TeacherMapper teacherMapper;

    //更新
    @RequestMapping("/toupdate")
    public String toupdate(Model model, int id) {   //更新的第一种取前端传来的值:a标签的th:href="@{/toupdate/(id=)}"相当于/toupdate?id=
        Teacher tea = teacherMapper.queryTeaById(id);
        model.addAttribute("tea", tea);
        return "toupdate.html";
    }

    @RequestMapping("/toupdate2/{id}")
    public String toupdate2(@PathVariable("id") Integer id, Model model) {//对应a标签 th:href="@{/toupdate2/}+${list.id}
        Teacher tea = teacherMapper.queryTeaById(id);
        model.addAttribute("tea", tea);
        return "toupdate.html";
    }

    @RequestMapping("/doupdate")
    public String douodate(Model model, Teacher teacher) {
        int i = teacherMapper.updateTea(teacher);
        if (i > 0) {
            return "redirect:/tomain";
        } else {
            return "redirect:/toupdate";
        }
    }
}
